package com.umpay.service.trans.impl;


import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.umpay.dao.offline.TransOffDao;
import com.umpay.dao.online.TransDao;
import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.entity.Trans;
import com.umpay.exception.BusinessException;
import com.umpay.service.trans.TransService;

@Service
public class TransServiceImpl implements TransService {

	private static final Logger log = LogManager.getLogger(TransServiceImpl.class);
	
	
	@Autowired
	private TransDao consumTransOnlineDao;
	@Autowired
	private TransOffDao consumTransOfflineDao;
	@Override
	public void insert(Trans consumeTrans) throws BusinessException{
		log.info(String.format("准备插入交易表数据：%s", consumeTrans));
        try {
            consumTransOnlineDao.insert(consumeTrans);
        } catch (DataAccessException e) {
            if (e instanceof DuplicateKeyException) {
                log.error(String.format("【交易表】插入主键冲突,插入交易信息[%s]",consumeTrans));
                throw new BusinessException(MerBusiEC.DB_DUPLICATE_KEY,"商户支付流水重复");
            }
            log.error("插入交易表失败", e);
            throw new BusinessException(MerBusiEC.DB_ERROR, "数据库操作失败");
        } catch (Exception e) {
            log.error("插入交易表失败", e);
            throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统发生错误！");
        }
		log.info(String.format("交易表插入成功！"));
	}

	@Override
	public void update(Trans consumTrans) {
		log.info(String.format("准备插入快捷消费交易表数据：%s", consumTrans));
		int result=-1;
		try{
			result = consumTransOnlineDao.update(consumTrans);
			/*if(result!=1){
				throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
			}*/
		}catch (DataAccessException e) {
			log.error("插入快捷消费订单表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("插入绑卡订单表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
		log.info(String.format("更新交易表成功！"));
	}

	@Override
	public Trans findTrans(Trans trans) {
		log.info(String.format("查询交易表数据：%s", trans));
		try{
			return consumTransOnlineDao.find(trans);
		}catch (DataAccessException e) {
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
	}
	@Override
	public Trans findOffLineTrans(Trans trans) {
		log.info(String.format("查询交易表数据：%s", trans));
		try{
			return consumTransOfflineDao.find(trans);
		}catch (DataAccessException e) {
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
	}
	@Override
	public Trans searchOffLineTrans(Trans trans) {
		log.info(String.format("查询交易表数据：%s", trans));
		try{
			return consumTransOfflineDao.searchTrans(trans);
		}catch (DataAccessException e) {
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
	}
	
	@Override
	public List<Trans> getTrans(Trans trans) {
		log.info(String.format("查询交易表数据：%s", trans));
		try{
			return consumTransOnlineDao.query(trans);
		}catch (DataAccessException e) {
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
		}catch(Exception e){
			log.error("查询交易表失败",e);
			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
		}
	}

    public List<Trans> getTransNoneErr(Trans trans) {
        log.info(String.format("查询交易表数据：%s", trans));
        try{
            return this.getTrans(trans);
        }catch (DataAccessException e) {
            log.error("查询交易表失败",e);
//			throw new BusinessException(MerBusiEC.DB_ERROR,"数据库操作失败");
            return null;
        }catch(Exception e){
            log.error("查询交易表失败",e);
//			throw new BusinessException(MerBusiEC.SYSTEM_ERR,"系统发生错误！");
            return null;
        }
    }


}
